# -*- coding: utf-8 -*-
# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
# pipelines是默认不开启的，需要取settings里面去开启
class DangdangwangPipeline(object):

    def process_item(self, item, spider):  # 这个方法的名字也不能改，改了之后无法经过这个管道Dangdangwangipeline来处理的
        #conn = pymysql.connect(host = "127.0.0.1",user="root",password="123456",database="dangdang",charset="utf8")
        conn = pymysql.connect(host="localhost", user="root", password="123456", database="dangdang", charset="utf8")
        cursor = conn.cursor()
        for i in range(0, len(item["title"])):
            try:
                title = item["title"][i]
                link = item["link"][i]
                comment = item["comment"][i]
                #print(title+":"+link+":"+comment)
                sql = "insert into sheet(title,link,comment) values('" + title + "','" + link + "','" + comment + "');"
                #print(sql)
                cursor.execute(sql)
                conn.commit()
            except Exception as err:
                print(err)
        cursor.close()
        conn.close()
        return item
